package com.quick.mail;


import com.quick.domain.Mail;
import com.quick.domain.MailQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName ConsumeMailQueue
 * @Description 邮件消费队列
 * @Author Wangxin
 * @Date 2019-03-02 21:38
 * @Version 1.0
 **/
@Component
@Order(value = 1)
@Slf4j
public class ConsumeMailQueue implements ApplicationRunner {
    @Resource
    private MailService mailService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("start");
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new ConsumeThread(mailService));
        executorService.execute(new ConsumeThread(mailService));
        log.info("ConsumeMailQueue start");
    }


}
class ConsumeThread implements Runnable{
    private MailService mailService;
    public ConsumeThread(MailService mailService){
        this.mailService = mailService;
    }
    @Override
    public void run() {
        for (;;){
            try {
                Mail mail = MailQueue.getInstance().take();
                System.out.println("消费消息");
                mailService.sendFreemarker(mail);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
